package mymail;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

public class POP3 {
    public static Folder getFolder(String host, String username, String password) {
   
        Properties props = new Properties();      
        props.put("mail.smtp.host", "dinosaur.com"); // smtp服务器      
        props.put("mail.smtp.auth", "true"); // 是否smtp认证      
        props.put("mail.smtp.port", "25"); // 设置smtp端口      
        props.put("mail.transport.protocol", "smtp"); // 发邮件协议      
        props.put("mail.store.protocol", "pop3"); // 收邮件协议      
        // 获取会话      
        Session session = Session.getInstance (props, null);      
        // 获取Store对象，使用POP3协议，也可能使用IMAP协议      
        System.out.println(host+" "+username+" "+ password);
        try {
            Store store = session.getStore("pop3");
            store.connect(host, username, password);
            Folder folder = store.getFolder("inbox");
            folder.open(Folder.READ_WRITE);
            Message message[] = folder.getMessages();      
            for (int i = 0; i < message.length; i++) {      
                // 打印出每个邮件的发件人和主题      
                System.out.println(i + ":" + message[i].getFrom()[0] + "\t"     
                        + message[i].getSubject()+"  "+message[i].getReceivedDate());      
            }    
            return folder;
        } catch (Exception e) {
            e.printStackTrace();
            
        }

        return null;
    }
}
